package com.shushan.db;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.shushan.base.ActivityStackManager;
import com.shushan.base.BaseActivity;
import com.shushan.base.Constants;
import com.shushan.lockscreen.bean.GameTimeBean;
import com.shushan.login.LoginOrRegisterActivity;
import com.shushan.login.bean.ChildBean;
import com.shushan.login.bean.ChildBookBean;
import com.shushan.login.bean.UserBean;
import com.shushan.manager.bean.RelaxTimeBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.util.SharedPreferencesUtils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: DBHelper
 * @Desciption: 数据库帮助类
 * @author: yichaohua
 * @date: 2018-02-08
 */
public class DBHelper extends OrmLiteSqliteOpenHelper {

    private static final String TAG = "DBHelper";

    /**
     * 数据库名字
     */
    private static final String DB_NAME = "smartstudy.db";
    /**
     * 数据库版本
     */
    private static final int DB_VERSION = 5;

    /**
     * 用来存放Dao的Map
     */
    private Map<String, Dao> daos = new HashMap<String, Dao>();



    private static DBHelper instance;
    /**
     * 获取单例
     * @param context
     * @return
     */
    public static synchronized DBHelper getHelper(Context context) {
        context = context.getApplicationContext();
        if (instance == null) {
            synchronized (DBHelper.class) {
                if (instance == null) {
                    instance = new DBHelper(context);
                }
            }
        }
        return instance;
    }


    /**
     * 构造方法
     * @param context
     */
    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        // 创建表
        try {
            TableUtils.createTable(connectionSource, ChildBean.class);
            TableUtils.createTable(connectionSource, UserBean.class);
            TableUtils.createTable(connectionSource, GameTimeBean.class);
            TableUtils.createTable(connectionSource, RelaxTimeBean.class);
            TableUtils.createTable(connectionSource, ChildBookBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        BaseActivity baseActivity = (BaseActivity) ActivityStackManager.getActivityStackManager().currentActivity();
        SharedPreferencesUtils.setBooleanDate(Constants.IS_LOGIN_SHAREPREFERS_KEY,false);
        if(baseActivity != null){
            baseActivity.startActivity(LoginOrRegisterActivity.class);
        }else {
            BabyApplication babyApplication = BabyApplication.getInstance();
            Intent intent = new Intent(babyApplication, LoginOrRegisterActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            babyApplication.startActivity(intent);
        }

        try {
            TableUtils.dropTable(connectionSource, ChildBean.class, true);
            TableUtils.dropTable(connectionSource, UserBean.class, true);
            TableUtils.dropTable(connectionSource, GameTimeBean.class, true);
            TableUtils.dropTable(connectionSource, RelaxTimeBean.class, true);
            TableUtils.dropTable(connectionSource, ChildBookBean.class, true);
            onCreate(database, connectionSource);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 通过类来获得指定的Dao
     */
    public synchronized Dao getDao(Class clazz) throws SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();
        if (daos.containsKey(className)) {
            dao = daos.get(className);

        }

        if(dao == null){
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }

        return dao;
    }


    /**
     * 释放资源
     */
    @Override
    public void close() {
        super.close();

        for (String key : daos.keySet()) {
            Dao dao = daos.get(key);
            dao = null;
        }
    }
}
